package com.example.my_rabbitmq_high_demo.api.limit;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Description: 消费者
 * @Author: GaoJian
 * @Date: 2022/7/9 11:02
 */
public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1、创建ConnectionFactory
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setVirtualHost("/");

        //2、创建连接
        Connection connection = factory.newConnection();

        //3、创建channel
        Channel channel = connection.createChannel();

        //4、声明exchange
        String exchangeName = "test_limit_exchange";
        String exchangeType = "topic";
        String routingKey = "limit.*";
        channel.exchangeDeclare(exchangeName, exchangeType, true, false, null);

        //5、声明消息队列
        String queueName = "test_limit_queue";
        channel.queueDeclare(queueName, true, false, false ,null);

        //6、绑定队列和exchange
        channel.queueBind(queueName, exchangeName, routingKey);

        //7、表示不限制消息大小，一次只处理一条消息，限制只是当前消费者有限
        channel.basicQos(0, 1, false);

        //8、设置消费者为自定义的消费者，要进行限流必须关闭自动签收
        channel.basicConsume(queueName, false, new MyConsumer(channel));
    }
}
